package com.future.mashibing.class02;

/**
 * Description:
 *
 * @author weiruibai.vendor
 * Date: 2021/9/9 14:27
 */
public class Code01_RandToRand {

    public static void main(String[] args) {
        int countTimes = 10000000;
        int[] ints = new int[6];
        for (int i = 0; i < countTimes; i++) {
            ints[f()]++;
        }
        System.out.println(ints[0] + " 出现概率：" + (double) ints[0] / (double) countTimes);
        ints = new int[10];
        for (int i = 0; i < countTimes; i++) {
            ints[f4()]++;
        }
        for (int i = 0; i < ints.length; i++) {
            System.out.println(i + "出现的概率：" + (double) ints[i] / (double) countTimes);
        }

    }

    /**
     * 等概率返回0、1
     *
     * @return
     */
    private static int f() {
        return Math.random() < 0.5D ? 0 : 1;
    }

    /**
     * 等概率返回1-5
     *
     * @return
     */
    private static int f2() {
        return (int) (Math.random() * 5) + 1;
    }

    /**
     * 等概率返回0-5
     *
     * @return
     */
    private static int f3() {
        return (int) (Math.random() * 6);
    }


    /**
     * 等概率返回1-7
     *
     * @return
     */
    private static int f4() {
        return (int) (Math.random() * 8) + 1;
    }
}
